// // //配置根路径
// axios.defaults.baseURL = 'http://124.223.14.236:8060'

const id = localStorage.getItem('index-id')
function render() {
    axios.get(`http://124.223.14.236:8060/index/show?id=${id}`).then((res) => {
        console.log(res)
        const { author, create_date, catename, title, click, content, lovenum } = res.data.data.info
        const strHeader = `
    <div class="breadcrumbs">
        <span class="text-muted">当前位置：</span>
        <a href="">优点达资讯系统</a>
        <small>></small>
        <a href="">${catename}</a> <small>></small>
        <span class="text-muted">正文</span>
    </div>
    <h1 class="article-title"><a href="">${title}</a></h1>
    <ul class="article-meta">
        <li><a href="">${author}</a> 发布于${create_date} </li>
        <li>分类：<a href="" rel="category tag">${catename}</a></li>
        <li><span class="post-views">阅读(${click})</span></li>
        <li>评论(1)</li>
        <li></li>
    </ul>
    `

        //页面的内容
        const strconten = content

        //版权描述
        const copyright = `
        <a href="">优点达资讯</a> &raquo; <a href="">${title}</a>
        `

        //点赞
        const social =`
        <a href="javascript:;" class="action action-like">
        <i class="glyphicon glyphicon-thumbs-up"></i>赞 (<span>${lovenum}</span>)
        </a> 
        `
    //     const newnavprev = `
    //     <span data-id="${data.data.prev[0].id}" id="upid">上一篇</span>
    //     <a href=" " rel="prev">${data.data.prev[0].title}</a>
    // `

    //     // 下一篇
    //     const newnavnext = `
    //     <span data-id="${data.data.next[0].id}" id="downid">下一篇</span>
    //     <a href=" " rel="next">${data.data.next[0].title}</a>
    //     `
    
    // 上一篇
    const newnavprev = `
       <span data-id="${res.data.data.prev[0].id}" id="upid">上一篇</span>
       <a href=" " rel="prev">${res.data.data.prev[0].title}</a>
   `
    // console.log(data.data.prev[0].id)




    // 下一篇
    const newnavnext = `
    <span data-id="${res.data.data.next[0].id}" id="downid">下一篇</span>
    <a href=" " rel="next">${res.data.data.next[0].title}</a>
    `
        

        


        $('.article-header').html(strHeader)
        $('.article-content').prepend(strconten)
        $('.article-social').html(social)
        $('.post-copyright').html(copyright)
        $('.article-nav.article-nav-prev').html(newnavprev)
        $('.article-nav.article-nav-next').html(newnavnext)

    })

}
render()

//右侧热门文章
function getTitle(){
    axios.get('http://124.223.14.236:8060/index/recommend').then((res)=>{
        // console.log(res)

        let newTitle = res.data.data.list.map((item)=>{
            return`
            <li>
              <a target="_blank" href="">
                <span class="thumbnail">
                  <img src="http://124.223.14.236:8060/${item.pic}" class="thumb" />
                </span>
                <span class="text">${item.title}</span>
                <span class="text-muted post-views">阅读(${item.click})</span>
              </a>
            </li>
            `
        })
        $('.items-02').html(newTitle.join(''))
    })
}
getTitle()

//右侧热门标签
function getTag(){
    axios.get('http://124.223.14.236:8060/index/index').then((res)=>{
        console.log(res)

        let newTag = res.data.data.allTag.map((item)=>{
            return`
            <li><a title="" href="">${item.tagname}</a></li>
            `
        })
        $('.widget_tags_inner').html(newTag.join(''))
    })
}
getTag()

//侧边图片
function getPic(){
    axios.get('http://124.223.14.236:8060/index/index').then((res)=>{
        console.log(res)

        let newPic = res.data.data.banner.map((item)=>{
            return`
            <div class="swiper-slide">
            <dic class="focusmo">
            <ul>
              <li class="large">
              <a target="_blank" href="${item.advimglink}">
                <img class="thumb" src="http://124.223.14.236:8060/${item.advimgsrc}">
                <h4>${item.advimgdesc}</h4>
              </a>
            </li>
            </ul>
            </div>
            </div>
            `
        })
        $('.swiper-wrapper').html(newPic.join(''))
    })
}
getPic()

//右侧广告
function getAll(){
    axios.get('http://124.223.14.236:8060/index/index').then((res)=>{
        // console.log(res)

        let newArr = res.data.data.sideBanner.map((item)=>{
            return`
            <a href="${item.advimglink}" rel="nofollow" target="_blank" title="${item.advimgdesc}">
            <img class="g430" style="width:360px;height:133px;" src="http://124.223.14.236:8060/${item.advimgsrc}" alt="" >
          </a>
            `
        })
        $('.textwidget').html(newArr.join(''))
    })
}
getAll()

//友情链接
function getAllLink(){
    axios.get('http://124.223.14.236:8060/index/index').then((res)=>{
        console.log(res)

        let newAllLink = res.data.data.allLink.map((item)=>{
            return`
           
            <li>
            <a href="${item.url}"><img src="http://124.223.14.236:8060/${item.img}" alt=""><p>${item.des}</p></a>
          </li>
         
            `
        })
        $('.widget.widget_comments ul').html(newAllLink.join(''))
    })
}
getAllLink()

//相关推荐
function getDemo(){
    axios.get(` http://124.223.14.236:8060/index/show?id=${id}`).then((res)=>{
        console.log(res)

        let newDemo = res.data.data.recommend.map((item)=>{
            return`
           
            <li>
            <a target="_blank" href="">
              <span>
                <img src="http://124.223.14.236:8060/${item.pic}" />
              </span>
              ${item.title}
            </a>
          </li>
         
            `
        })
        $('.relates-model-thumb').html(newDemo.join(''))
    })
}
getDemo()

//上一篇
function getPrev(){
    axios.get(`http://124.223.14.236:8060/index/show?id=${id}`).then((res)=>{
        console.log(res)

        let newPrev = res.data.data.prev.map((item)=>{
            return`
            <span data-id="${res.data.data.prev[0].id}" id="upid">上一篇</span>
            <a href="" rel="prev">${item.title}</a>
         
            `
        })
        $('.article-nav-prev').html(newPrev.join(''))
    })
}
getPrev()

//下一篇
function getNext(){
    axios.get(` http://124.223.14.236:8060/index/show?id=${id}`).then((res)=>{
        console.log(res)

        let newNext = res.data.data.next.map((item)=>{
            return`
            <span data-id="${res.data.data.next[0].id}" id="downid">下一篇</span>
            <a href="" rel="prev">${item.title}</a>
         
            `
        })
        $('.article-nav-next').html(newNext.join(''))
    })
}
getNext()

// 上一页
$(".article-nav").on('click', '.article-nav-prev', function () {
    const id = $('#upid').attr('data-id')
    // console.log(id)
    localStorage.setItem('index-id', id)
    if (+localStorage.getItem('index-id') === id) {
        console.log(1)
        return
    } else {
        console.log(2)
        render()
    }
})



// 下一页
$(".article-nav").on('click', '.article-nav-next', function () {
    const id = $('#downid').attr('data-id')
    // console.log(id)
    localStorage.setItem('index-id', id)
    if (localStorage.getItem('index-id') === id) {
        return
    } else {
        // 渲染页面
        render()
    }
})

// 保存到本底存储用show页面的使用
$('#article').on('click', '.excerpt-one', function () {
    const id = $(this).attr('data-id')
    localStorage.setItem('index-id', id)
    location.href = './show.html'
  })

